Mybatis in 条件传参三种实现方法(直接$,List,[]) 您所在的位置:网站首页 sql in 参数化 Mybatis in 条件传参三种实现方法(直接$,List,[])

Mybatis in 条件传参三种实现方法(直接$,List,[])

2024-01-31 00:01| 来源: 网络整理| 查看: 265

第一种方法:in 条件为拼接好的字符串

如果直接传入拼接好的where in 条件, 比如('111','222','333'),则需要使用${idlist}传参,即绝对引用,而不能使用# , 如果使用#传参会被mybatis当成字符串再添加一层''引号,导致错误. 优点:简单方便,高效,缺点:不能防止SQL注入

第二种方法:in 条件为List对象   in条件直接传入List对象,让mybatis再去拼接生成in条件,这个很麻烦,但是可以防止SQL注入

第三种方法:in 条件为String[] 数组

in条件直接传入[]数组对象,让mybatis再去拼接生成in条件,这个很麻烦,但是可以防止SQL注入

如果项目大,其实可以同时重载三种都实现,我一般都会这样,实现三种DAO接口,service层相同方法名,根据不同的模块不同的需求调用不同的实现层

Service:

int deleteMenuByIdList(String idlist,int delcount,int lastsort); int deleteMenuByIdList(List idlist, int delcount,int lastsort); int deleteMenuByIdList(String[] idlist, int delcount,int lastsort);

Dao:

//用这种写法方便,idlist直接拼接好,xml中用 in ${idlist}接受参数 int deleteMenuByIdList(@Param("idlist")String idlist, @Param("delcount")int delcount, @Param("lastsort")int lastsort); //用这种写法直接传List对象,xml中再写循环拼接,麻烦 int deleteMenuByIdList2(@Param("idlist")List idlist, @Param("delcount")int delcount, @Param("lastsort")int lastsort); //用这种写法直接传String[]数组,xml中再写循环拼接,麻烦 int deleteMenuByIdList3(@Param("idlist")String[] idlist, @Param("delcount")int delcount, @Param("lastsort")int lastsort);

(2,3)的xml文件中不需要做修改,只需要修改一下id对应到DAO的方法名即可。

mappper.xml

1, delete from s_menu where menu_id in ${idlist}; update s_menu set sort=sort-#{delcount} where sort >= #{lastsort} and menu_id not in ${idlist}; 2, delete from s_menu where menu_id in #{menu_id} ;update s_menu set sort=sort-#{delcount} where sort >= #{lastsort} and menu_id not in #{menu_id} ; 3, delete from s_menu where menu_id in #{menu_id} ;update s_menu set sort=sort-#{delcount} where sort >= #{lastsort} and menu_id not in #{menu_id} ;



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有